﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_DLL.Product.DAO;
using ERP_RORZE_DLL.Product.database;
using ERP_RORZE_DLL.Product.Base;
using ERP_RORZE_PERMISSION.Base;

namespace ERP_RORZE_PRODUCT.Product.Quotation
{
    public partial class ctrlQuatationList : DevExpress.XtraEditors.XtraUserControl
    {
        ClsQuationDAO clsQuationDAO = new ClsQuationDAO();
        DataTable dt;
        QuotationTable b = new QuotationTable();
        private LogUtils log;

        public ctrlQuatationList()
        {
            InitializeComponent();
            log = new LogUtils();
        }

        void Reload(object sender, EventArgs e)
        {
            dt = clsQuationDAO.GetAllQuotation();
            gridControl1.DataSource = dt;
            gridView1.BestFitColumns();
        }
        private void ctrlQuatationList_Load(object sender, EventArgs e)
        {
            Reload(sender, e);
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            frm_QuotationAdd frmAdd = new frm_QuotationAdd();
            frmAdd.Reload += new EventHandler(Reload);
            frmAdd.Show();
        }

        private void btnReload_Click(object sender, EventArgs e)
        {
            Reload(sender, e);
        }

        private void BtnDelete_Click(object sender, EventArgs e)
        {
            b.QuotationID = gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"QuotationID").ToString();
            clsQuationDAO.DeleteQuotation(b);

            //write Log
            log.Table = LogConstant.Table.Product.QuotationList;
            log.Task = LogConstant.Task.Delete;
            log.Description = "QuotationID: " + b.QuotationID;
            log.WriteLog();

            Reload(sender, e);
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            try
            {
                frm_QuotationEdit frmEdit = new frm_QuotationEdit();
                frmEdit.QuotationID = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "QuotationID").ToString();
                frmEdit.Date = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Date").ToString();
                frmEdit.CustomerID = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "CustomerID").ToString();
                frmEdit.Reload += new EventHandler(Reload);
                frmEdit.Show();
            }
            catch
            {
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            Reload(sender, e);
        }

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            btnAdd_Click(sender, e);
        }

        private void btEdit_Click(object sender, EventArgs e)
        {
            btnEdit_Click(sender, e);
        }

        private void btDelete_Click(object sender, EventArgs e)
        {
            BtnDelete_Click(sender, e);
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            clsConstantsFuction.ExportToExcel(gridView1, 1, true);
        }
    }
}
